<template>
  <el-form label-width="100px">
    <el-form-item label="异常处理" prop="exceptionEvent">
      <el-select
          v-model="exceptionEventForm.exceptionEvent"
          clearable
          style="width: 100%"
          @change="changeExceptionEvent"
      >
        <el-option
          v-for="key in Object.keys(ExceptionEventType)"
            :key="key"
            :label="ExceptionEventType[key]"
            :value="key"
        />
      </el-select>
    </el-form-item>
    <el-form-item
        v-if="exceptionEventForm.exceptionEvent == 'toUser'"
        label="指定用户"
        prop="exceptionEventToUser"
        span="24"
    >
      <el-select
          v-model="exceptionEventForm.exceptionEventToUser"
          clearable
          multiple
          style="width: 100%"
          @change="updateElementTask"
      >
        <el-option
            v-for="item in userOptions"
            :key="item.id"
            :label="item.nickname"
            :value="item.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import * as UserApi from '@/api/system/user'
import { ExceptionEventType } from './utilSelf'
import {
    createExceptionConfigObject,
    updateElementExtensions
} from "@/components/bpmnProcessDesigner/package/utils";

defineOptions({ name: 'ExceptionConfig' })
const props = defineProps({
  id: String,
  type: String
})
const exceptionEventForm = ref({
  exceptionEvent: undefined, // 分配规则
  exceptionEventToUser: [] // 分配选项
})

const prefix = inject('prefix')

const bpmnElement = ref()
const bpmnInstances = () => (window as any)?.bpmnInstances

const userOptions = ref<UserApi.UserVO[]>([]) // 用户列表


const resetTaskForm = () => {
    let exceptionConfigList = bpmnElement.value.businessObject?.extensionElements?.values?.filter(
      (ex) => ex.$type === `${prefix}:ExceptionConfig`
  ) ?? []

    if( exceptionConfigList.length > 0 ){
        const temp = exceptionConfigList[0] ;
        if (temp.exceptionEvent != undefined) {
            exceptionEventForm.value.exceptionEvent = temp.exceptionEvent as string
        } else {
            exceptionEventForm.value.exceptionEvent = undefined
        }
        if (temp.exceptionEventToUser != undefined && temp.exceptionEventToUser.length > 0) {
            exceptionEventForm.value.exceptionEventToUser = temp.exceptionEventToUser
                .split(',')
                .map((item) => +item)
        } else {
            exceptionEventForm.value.exceptionEventToUser = undefined
        }
    }

}

const changeExceptionEvent = () => {
  exceptionEventForm.value.exceptionEventToUser = []
  updateElementTask()
}

/** 选中某个 options 时候，更新 bpmn 图  */
const updateElementTask = () => {

    const exceptionConfigObject = createExceptionConfigObject(exceptionEventForm.value,  prefix)

    updateElementExtensions(
        bpmnElement.value,
        [exceptionConfigObject]
    )

}


watch(
    () => props.id,
    () => {
      bpmnElement.value = bpmnInstances().bpmnElement
      nextTick(() => {
        resetTaskForm()
      })
    },
    { immediate: true }
)

onMounted(async () => {
  // 获得用户列表
  userOptions.value = await UserApi.getSimpleUserList()
})

onBeforeUnmount(() => {
  bpmnElement.value = null
})
</script>
